<?php

namespace app\admin\model;

use ba\Random;
use think\Model;
use think\model\relation\BelongsTo;
use app\admin\model\GoodsSpecs as specs;

/**
 * Goods 模型
 */
class Goods extends Model
{
    protected $autoWriteTimestamp = true;

    public function getAvatarAttr($value): string
    {
        return full_url($value, false, config('buildadmin.default_avatar'));
    }

    public function setAvatarAttr($value): string
    {
        return $value == full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
    }

    public function getMoneyAttr($value): string
    {
        return bcdiv($value, 100, 2);
    }

    public function setMoneyAttr($value): string
    {
        return bcmul($value, 100, 2);
    }


    public function cat(): BelongsTo
    {
        return $this->belongsTo(GoodsCat::class, 'cat_id');
    }


      protected static function onAfterInsert($model)
    {   
          $guan_id = $model->getData("id");
          $guige = $model->guige;
          if(isset($guige) && $model->specs == 1){
              $min = 99999;
              $old_pc = '';
              $stock = 0;
              foreach($guige as &$v){
                      $arr = [];
                      $arr['price'] = $v['price'];
                      $arr['origin_price'] = $v['origin_price'];
                      $arr['stock'] = $v['stock'];
                      $arr['name'] = $v['name'];
                      $arr['goods_id'] = $guan_id;
                      $arr['pic'] = $v['pic'] ?? '';
                      specs::insert($arr);

                      $stock += intval($v['stock']);
                      if($v['price'] < $min){
                         $min = $v['price'];
                         $old_pc = $v['origin_price'];
                      }
              }

              $model->where("id",$guan_id)->update([
                    'stock'=>$stock,
                    'price'=>$min,
                    'origin_price'=>$old_pc
              ]);
          }
          //
    }

    protected static function onAfterUpdate($model){
            $guige = $model->guige;
            $guan_id = $model->getData("id");
            if(isset($guige)){
                        foreach($guige as &$v){
                                if(isset($v['id'])){
                                      $arr = [];
                                      $arr['price'] = $v['price'];
                                      $arr['origin_price'] = $v['origin_price'];
                                      $arr['stock'] = $v['stock'];
                                      $arr['name'] = $v['name'];
                                      $arr['pic'] = $v['pic'] ?? '';
                                      specs::where("id",$v['id'])->update($arr);     
                                }else{
                                      $arr = [];
                                      $arr['price'] = $v['price'];
                                      $arr['origin_price'] = $v['origin_price'];
                                      $arr['stock'] = $v['stock'];
                                      $arr['name'] = $v['name'];
                                      $arr['goods_id'] = $guan_id;
                                      $arr['pic'] = $v['pic'] ?? '';
                                      specs::insert($arr);
                                }     
                       } 
            }

    }



}